<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80"
      @submit="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }"
      @reset="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="项目名称" name="projectName">
            <t-input v-model="queryParams.projectName" clearable placeholder="请输入项目名称" style="width: 240px" />
          </t-form-item>
          <t-form-item label="项目编号" name="projectNum">
            <t-input v-model="queryParams.projectNum" clearable placeholder="请输入项目编号" style="width: 240px" />
          </t-form-item>
          <t-form-item label="承担单位" name="declareDeptName">
            <t-input v-model="queryParams.declareDeptName" clearable placeholder="请输入承担单位" style="width: 240px" />
          </t-form-item>
          <t-form-item label="归口管理单位" name="managerDeptName">
            <t-input v-model="queryParams.managerDeptName" clearable placeholder="请输入归口管理单位" style="width: 240px" />
          </t-form-item>
          <t-form-item v-if="site_tag === 'jtt'" label="计划类别" name="projectCategory">
            <t-tree-select filterable expand-all clearable v-model="queryParams.projectCategory" style="width: 240px"
              :treeProps="{ keys: { value: 'id', label: 'name' } }" :data="projectCategoryOptions"></t-tree-select>
          </t-form-item>
          <t-form-item label="学科分类" name="subjectClassification">
            <t-select style="width: 240px" clearable v-model="queryParams.subjectClassification"
              :options="dict.type.subject_classification">
            </t-select>
          </t-form-item>
          <t-form-item label="项目类型" name="projectType">
            <t-select style="width: 240px" clearable v-model="queryParams.projectType"
              :options="dict.type.project_type">
            </t-select>
          </t-form-item>
          <t-form-item label="项目状态" name="projectStatus">
            <t-select style="width: 240px" clearable v-model="queryParams.projectStatus"
              :options="dict.type.project_status">
            </t-select>
          </t-form-item>
          <t-form-item label="分配状态" name="isAddReview">
            <t-select style="width: 240px" clearable v-model="queryParams.isAddReview"
              :options="[{ value: '0', label: '未分配' }, { value: '1', label: '已分配' }]">
            </t-select>
          </t-form-item>
          <t-form-item label="形式审查状态" name="xsReviewStatus">
            <t-select v-model="queryParams.xsReviewStatus" style="width: 240px" clearable
              :options="dict.type.xs_review_status">
            </t-select>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="exportExcel" variant="outline" theme="primary">
        <t-icon name="download" slot="icon" />
        导出
      </t-button>
    </div>
    <t-tabs v-if="site_tag === 'jtt'" @change="(val) => { queryParams.projectCategory = val; getList(); }"
      v-model="queryParams.projectCategory" style="margin-bottom: 10px">
      <t-tab-panel :key="item.id" :value="item.id" v-for="(item) in [{ 'name': '全部', id: null }, ...projectCategoryOptions]"
        :label="item.name" :destroyOnHide="false">
      </t-tab-panel>
    </t-tabs>
    <t-table :sort="sort" :hideSortTips="true" @sort-change="sortChange" :pagination="pagination" row-key="id"
      :columns="site_tag === 'jtt' ? columns : columns.filter(val => { return val.title !== '计划类别' })" :data="data"
      :loading="dataLoading">
      <template #[`projectNum`]="{ row }">
        <div v-if="row.projectNum">
          {{ row.projectNum }}
        </div>
        <div v-else>
          暂未分配
        </div>
      </template>
      <template #projectName="{ row }">
        <t-link v-if="row.version === '2024'"
          @click="dialog.objId = row.id; dialog.header = row.projectName; dialog.open_item_detail = true; dialog.row = row"
          hover="color" theme="primary">{{ row.projectName }}</t-link>
        <t-link v-else @click="dialog.objId = row.id; dialog.header = row.projectName; dialog.open_detail = true" hover="color"
          theme="primary">{{ row.projectName }}</t-link>
      </template>
      <template #[`adviceFunding`]="{ row }">
        {{ row.adviceFunding }}万元
      </template>
      <template #[`subsidyFunding`]="{ row }">
        {{ row.subsidyFunding }}万元
      </template>
      <template #[`subjectClassification`]="{ row }">
        {{ row.subjectClassification | filterByDict(dict.type.subject_classification) }}
      </template>
      <template #projectType="{ row }">
        {{ row.projectType | filterByDict(dict.type.project_type) }}
      </template>
      <template #[`projectStatus`]="{ row }">
        <t-tag @click="dialog.header = row.projectName; dialog.objId = row.id; dialog.open_approve_list = true"
          class="cursor-pointer" :theme="handleTheme(row.projectStatus, dict.type.project_status)" variant="light">
          {{ row.projectStatus | filterByDict(dict.type.project_status) }}
        </t-tag>
      </template>
      <template #[`isAddReview`]="{ row }">
        <t-tag variant="light" theme="success" v-if="row.isAddReview === '1'">
          已分配
        </t-tag>
        <t-tag variant="light" theme="warning" v-else>
          未分配
        </t-tag>
      </template>
      <template #[`xsReviewStatus`]="{ row }">
        <t-tag v-if="row.xsReviewStatus"
          @click="dialog.header = row.projectName; dialog.objId = row.id; dialog.open_approve_list = true"
          class="cursor-pointer" :theme="handleTheme(row.xsReviewStatus, dict.type.xs_review_status)" variant="light">
          {{ row.xsReviewStatus | filterByDict(dict.type.xs_review_status) }}
        </t-tag>
        <t-tag @click="dialog.header = row.projectName; dialog.objId = row.id; dialog.open_approve_list = true"
          class="cursor-pointer" v-else theme="warning" variant="light">未评审</t-tag>
      </template>

      <template #op="{ row }">
        <!--        <t-button  size="small" @click="dialog.objId=row.id;dialog.open_select_approver=true" variant="text" class="t-button-link">-->
        <!--          <t-icon name="add" slot="icon"/>-->
        <!--          分配形式审查人员-->
        <!--        </t-button>-->
        <t-button v-if="row.version === '2024'"
          @click="dialog.objId = row.id; dialog.header = row.projectName; dialog.open_item_detail = true; dialog.row = row"
          size="small" variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon" />
          详情
        </t-button>
        <t-button v-else @click="dialog.objId = row.id; dialog.header = row.projectName; dialog.open_detail = true;"
          size="small" variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon" />
          详情
        </t-button>
        <t-button v-if="row.isAddReview === '0'" theme="danger" size="small"
          @click="dialog.row = row; dialog.open_approve_back = true" variant="text" class="t-button-link">
          <t-icon name="rollback" slot="icon" />
          退回
        </t-button>
      </template>
    </t-table>
    <project-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_detail"
      v-if="dialog.open_detail"></project-detail-dialog>
    <item-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_item_detail"
      :row="dialog.row" v-if="dialog.open_item_detail"></item-detail-dialog>
    <select-approver header="选择形式审查人员" :objId="dialog.objId" :visible.sync="dialog.open_select_approver"
      v-if="dialog.open_select_approver"></select-approver>
    <approve-list :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_approve_list"
      v-if="dialog.open_approve_list"></approve-list>
    <approve-back :visible.sync="dialog.open_approve_back" @reload="getList()" :row="dialog.row"
      v-if="dialog.open_approve_back"></approve-back>
  </div>

</template>

<script>
import SelectApprover from "./components/select-approver.vue"
import ApproveList from "./components/approve-list.vue";
import ApproveBack from "./components/approve-back.vue";
import ProjectDetailDialog from "../pm/components/project-detail-dialog.vue";
import ItemDetailDialog from "../pm/components/item-detail-dialog.vue";

export default {
  name: "approve-project",
  dicts: ["project_type", "subject_classification", "sys_user_sex", "prof_tit", "degree", "duty", "platform_type", "member_type", "project_status", "xs_review_status"],
  components: {
    SelectApprover,
    ApproveList,
    ApproveBack,
    ProjectDetailDialog,
    ItemDetailDialog
  },
  data () {
    return {
      site_tag: import.meta.env.VITE_SITE_TAG,
      sort: [],
      projectCategoryOptions: [],
      dialog: {
        open: false,
        header: "",
        row: {},
        data: {},
        open_project_detail: false,
        open_outline_detail: false,
        open_contract_detail: false,
        objId: 0,
        row: {},
        open_approve_list: false,
        open_select_approver: false,
        open_detail: false,
        open_approve_back: false,
        open_item_detail: false,
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectCategory: null,
        projectName: '',
        projectType: '',
        subjectClassification: '',
        dateRange: [],
        sortGroup: '',
        deptName: '',
        projectStatus: '',
        isAddReview: '',
        xsReviewStatus: '',
        declareDeptName: '',
        managerDeptName: ''
      },
      data: [],
      columns: [
        {
          colKey: "projectName",
          title: "项目名称",
          align: "center",
          width: 220,
          // sorter: true,
        },
        // {
        //   colKey: "declareDate",
        //   title: "申报日期",
        //   align: "center",
        //   width: 120,
        //   sorter: true,
        // },
        {
          colKey: "adviceFunding",
          title: "建议经费总概算",
          align: "center",
          width: 160,
          sorter: true,
        },
        {
          colKey: "subsidyFunding",
          title: import.meta.env.VITE_SITE_TAG === 'jtt' ? "厅拨经费" : `集团补助经费`,
          align: "center",
          width: 140,
          sorter: true,
        },
        {
          colKey: "projectLeaderName",
          title: "负责人",
          align: "center",
          width: 100,
        },
        {
          colKey: "declareDeptName",
          title: "承担单位",
          align: "center",
          width: 180,
          // sorter: true,
        },
        {
          colKey: "managerDeptName",
          title: "归口单位",
          align: "center",
          width: 180,
        },
        {
          colKey: "projectCategoryName",
          title: "计划类别",
          align: "center",
          width: 100,
        },
        {
          colKey: "subjectClassification",
          title: "学科分类",
          align: "center",
          width: 100,
        },
        {
          colKey: "projectType",
          title: "项目类型",
          align: "center",
          width: 120,
        },
        {
          colKey: "startDate",
          title: "开始时间",
          align: "center",
          width: 120,
          sorter: true,
        },
        {
          colKey: "endDate",
          title: "结束时间",
          align: "center",
          width: 120,
          sorter: true,
        },
        {
          colKey: "xsCreateTime",
          title: "创建时间",
          align: "center",
          width: 200
        },
        {
          colKey: "projectStatus",
          title: "项目状态",
          align: "center",
          width: 220,
          fixed: 'right',
          sorter: true
        },
        {
          colKey: "isAddReview",
          title: "分配状态",
          align: "center",
          // width:220,
          fixed: 'right',
          // sorter: true
        },
        {
          colKey: "xsReviewStatus",
          title: "形式审查状态",
          align: "center",
          width: 120,
          fixed: 'right',
          // sorter: true
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 120,
          fixed: 'right'
        },
      ],

      dataLoading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination, pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  async created () {
    Object.assign(this.queryParams, this.handelQuery(this.queryParams, this.$route.query))
    await this.getDeclareTree();
    this.getList()
  },
  methods: {
    exportExcel () {
      this.$download(`kjxm/excel/xsReviewList`, this.queryParams, `待形式审查项目列表_${new Date().getTime()}.xls`)
    },
    changeTab (value) {
      this.queryParams.projectCategory = value;
      this.getList();
    },
    getDeclareTree () {
      return this.$api.kjxm.project.planCategoryList().then(response => {
        this.projectCategoryOptions = this.handleTree(response.data, "id");
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    sortChange (sort) {
      this.sort = sort;
      let orderByColumn = []
      let isAsc = []
      if (sort instanceof Array) {
        orderByColumn = sort.map(val => val.sortBy)
        isAsc = sort.map(val => val.descending ? `desc` : `asc`)
      } else {
        orderByColumn = [sort.sortBy]
        isAsc = [sort.descending ? `desc` : `asc`]
      }
      this.queryParams.orderByColumn = orderByColumn.join(",")
      this.queryParams.isAsc = isAsc.join(",")
      this.getList();
    },
    getList () {
      this.dataLoading = true;
      this.$api.kjxm.approve.getApproverReviewAllList2(this.queryParams).then(response => {
        this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
  },
}
</script>

<style scoped></style>
